import cv2
import numpy as np
import matplotlib.pyplot as plt

# 读取图像
img = cv2.imread('99.jpg')

# 转换颜色空间为RGB
src = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# 获取图像的高度和宽度
height = src.shape[0]
width = src.shape[1]

# 计算每个采样块的高度和宽度
numHeight = height // 16
numWidth = width // 16

# 创建一幅新的图像用于存储采样后的结果
new_img = np.zeros((height, width, 3), np.uint8)

# 图像循环采样8x8的区域
for i in range(16):
    y = i * numHeight  # 获取 Y 坐标
    for j in range(16):
        x = j * numWidth  # 获取 X 坐标
        # 获取当前采样点的像素值
        b = src[y, x][0]
        g = src[y, x][1]
        r = src[y, x][2]
        # 循环设置区域采样
        for n in range(numHeight):
            for m in range(numWidth):
                new_img[y + n, x + m][0] = np.uint8(b)
                new_img[y + n, x + m][1] = np.uint8(g)
                new_img[y + n, x + m][2] = np.uint8(r)

# 设置标题和图像列表
titles = ['src', 'Sampling']
images = [src, new_img]

# 使用Matplotlib显示原始图像和采样后的图像
plt.figure(figsize=(10, 4))
for i in range(2):
    plt.subplot(1, 2, i + 1)
    plt.imshow(images[i])
    plt.title(titles[i])
    plt.axis('off')  # 关闭坐标轴

plt.show()
